package frist50;

import org.junit.jupiter.api.Test;

public class T0042 {
    public int trap(int[] height) {

        int len = height.length;
        int[][] ints = new int[len][2];
        int max = 0;
        int sum = 0;
        for (int i = 0; i < len; i++) {
            max = Math.max(max, height[i]);
            ints[i][0] = max;
        }
        max = 0;
        for (int i = len - 1; i >= 0; i--) {
            max = Math.max(max, height[i]);
            ints[i][1] = max;
        }
        for (int i = 0; i < len; i++) {
            max = Math.min(ints[i][0], ints[i][1]);
            sum += (max - height[i]);
        }
        return sum;
    }

    @Test
    public void test() {
        int[] h = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
        System.out.println(trap(h));
    }
}
